package com.example.homelicaisystem.services.impl;


import com.example.homelicaisystem.dao.UserMapper;
import com.example.homelicaisystem.entity.*;
import com.example.homelicaisystem.entity.DTO.UserDTO;
import com.example.homelicaisystem.entity.exception.*;
import com.example.homelicaisystem.services.UserService;
import com.example.homelicaisystem.utils.UserUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserImpl implements UserService {

    private static int CREATE = 0;
    private static int WEICHAT = 1;
    private static int ALIPAY = 2;
    private static int BANK = 3;
    private static int FamilyMangePower = 100;

    private static int MYSHARES = 1;
    private static int FUNDS = 2;
    private static int BOND = 3;
    private final UserMapper userMapper;

    public UserImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public User create(User data) {
//        创建不能使用重复电话
       if (userMapper.phoneExists(data.getPhone())){
           throw new PhoneException("此电话已被注册");
       }
        data.setName(data.getName().trim());
        data.setState(CREATE);

        userMapper.insertUser(data);

        User user = userMapper.getUserByPhone(data.getPhone());


        return data;
    }

    @Override
    public String login(User data) {
        User user = userMapper.login(data);
        //拿出user的phone和id 存入token
        if (user != null) {
            if (user.getPassword().equals(data.getPassword())) {
                UserUtil.saveUser(user.getId());
                return UserUtil.getToken(user.getId());
            } else {
                throw new RuntimeException("密码错误");
            }
        } else {
            throw new RuntimeException("此电话未注册");
        }
    }


    @Override
    public boolean hasPower(Integer userId, String permission) {
        return userMapper.hasPower(userId, permission);
    }

    @Override
    public List<Power> getPower() {
        int id = UserUtil.getUserId();
        return userMapper.getPower(id);
    }

    @Override
    public void increaseCash(Detail data) {

        User user = userMapper.getUserById(UserUtil.getUserId());

        if (data.getModeId() == WEICHAT) {
            user.setWeichat(user.getWeichat() + data.getMoney());
        }
        if (data.getModeId() == ALIPAY) {
            user.setAlipay(user.getAlipay() + data.getMoney());
        }
        userMapper.updateCash(user);
    }

    @Override
    public void decreaseCash(Detail data) {
        User user = userMapper.getUserById(UserUtil.getUserId());

        if (data.getModeId() == WEICHAT) {
            if (user.getWeichat() < data.getMoney()) {
                throw new UsedCashException("金额错误");
            }
            user.setWeichat(user.getWeichat() - data.getMoney());
        }
        if (data.getModeId() == ALIPAY) {
            if (user.getAlipay() < data.getMoney()) {
                throw new UsedCashException("金额错误");
            }
            user.setAlipay(user.getAlipay() - data.getMoney());
        }
        userMapper.updateCash(user);
    }

    @Override
    public User getUserById(Integer userId) {
        return userMapper.getUserById(userId);
    }

    @Override
    public User getUserByPhone(String phone) {
        return userMapper.getUserByPhone(phone);
    }

    @Override
    public void initializeUserPower(User user) {
        for(int powerId = 2;powerId< 5;powerId++) {
            userMapper.insertUserPower(user.getId(), powerId);
        }
        //初始注册用户获得添加家庭成员权限
        userMapper.insertUserPower(user.getId(),FamilyMangePower);
    }

    @Override
    public void deleteUserPower(int id) {
        userMapper.deleteUserPower(id);
    }



    @Override
    public void initializeFamilyMemberPower(User user) {
        for(int powerId = 2;powerId< 6;powerId++) {
            userMapper.insertUserPower(user.getId(), powerId);
        }
    }


    @Override
    public Page<User> getList(UserDTO data) {
        PageHelper.startPage(data.getPageNum(), data.getPageSize());
        return userMapper.getList(data);
    }

    @Override
    public List<SelectResult> select() {
        return userMapper.select();
    }

    @Override
    public void delete(int id) {
        if (!userMapper.idExists(id)) {
            throw new RuntimeException("id不存在");
        }
        userMapper.delete(id);
    }

    @Override
    public void modify(User data) {
        if (data.getName() == null) {
            throw new NoNameException("修改用户需要填写name");
        }
        if (data.getPhone() == null) {
            throw new NoPhoneException("修改用户需要填写phone");
        }
//        if(data.getPassword()==null){
//            throw new NoPasswordException("修改用户需要填写password");
//        }
        //格式化姓名
        data.setName(data.getName().trim());
        userMapper.update(data);
    }


}
